/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
/*
 * Consignee.java
 *
 * Created on Oct 27, 2010, 4:59:37 PM
 */

package lmclogistics;

import components.Text;
import components.TextArea;
import components.Image;
import components.NButton;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import sql.Sql;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Sonia Tan
 */
public class AddConsignee extends javax.swing.JFrame {

    /** Creates new form Trucking */

    TextArea firstNameField = new TextArea();
    TextArea lastNameField = new TextArea();
    private Image bg;

    public AddConsignee() {

        setTitle("Add Consignee");
        setAlwaysOnTop(true);
        setSize(300, 200);
        centerScreen();
        setVisible(true);
        setBg();
        okCancelButtons();
        initText();
        initFlds();
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLayout(null);

        BufferedImage image = null;
        try {
            image = ImageIO.read(getClass().getResource("/images/favicon.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        setIconImage(image);
    }

    public void setBg(){
        bg = new Image("panel.png",507,460);
        add(bg,-1);

    }

    public int getCenterX(int x){

        int a = 919/2 - x/2;


        return a;
    }

    public int getCenterY(int x){

        int a = 577/2 - x/2;


        return a;
    }

    public void okCancelButtons()
    {
                NButton cancel = new NButton("Cancel",200,130);
                NButton ok = new NButton("Ok",115 ,130);
                add(ok,0);
                add(cancel,0);
                cancel.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent e) {
                    dispose();
                    }
                });

                ok.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent e) {
                    //FormWarning warn = new FormWarning("incorrect");
                    addConsignee();
                    }
                });
    }

    public void addConsignee()
    {   try {
                String query = "INSERT INTO consignee(consFName, consLName,deleted) " +
                " VALUES('"+firstNameField.getText()+"','"+lastNameField.getText()+"',0)";


                Connection connection = Sql.getSqlConnection();
                Statement stmt = connection.createStatement();
                stmt.executeUpdate(query);


                Sql.closeSqlConnection(connection);

                } catch (Exception ex) {
                Logger.getLogger(AddConsignee.class.getName()).log(Level.SEVERE, null, ex);
                }
                this.dispose();
                
    }

    public void initText(){
        Text code = new Text();
        Text cd = new Text();
        Text firstName = new Text();
        Text lastName = new Text();

        code.inText("Code:", 15, 15);
        firstName.inText("First Name:",15,40);
        lastName.inText("Last Name:",15,65);

        add(code,0);
        add(cd,0);
        add(firstName,0);
        add(lastName,0);

        String codeVal = "SELECT MAX(consCode) FROM consignee";

        try{
            Connection connection = Sql.getSqlConnection();
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(codeVal);

            if(rs.next())
            {
                cd.inText(""+(Integer.parseInt(rs.getString(1))+1),100,15);
            }
        } catch(Exception ex)
        {
            Logger.getLogger(AddConsignee.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void initFlds() {
        firstNameField.initText(60, 25, 100, 38);
        lastNameField.initText(60, 25, 100, 63);

        add(firstNameField,0);
        add(lastNameField,0);

    }


    public void centerScreen(){

        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

        // Determine the new location of the window
        int w = getSize().width;
        int h = getSize().height;
        int x = (dim.width-w)/2;
        int y = (dim.height-h)/2;

        // Move the window
        setLocation(x, y);
    }







    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
